package com.wyb.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wyb.mapper.ArticleMapper;
import com.wyb.mapper.CategoryMapper;
import com.wyb.pojo.Article;
import com.wyb.pojo.Category;
import com.wyb.constant.Result;
import com.wyb.service.ArticlePageService;
import com.wyb.service.GetAuthorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/article")
/**
 * @description:文章操作get接口
 * @author: ruge
 */
public class ArticleListController {

    @Autowired
    private ArticlePageService articlePageService;

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private GetAuthorService getAuthorService;

    @Autowired
    private CategoryMapper categoryMapper;

    @GetMapping("/getarticlelist")
    public Result getArticleList(@RequestParam int current,
                                 @RequestParam int pagesize,
                                 @RequestParam int categoryId,
                                 @RequestHeader String token){
        return articlePageService.articlePages(current,pagesize,categoryId,token);
    }

    //文章作者信息
    @GetMapping("/getauthor")
    public Result getAuthor(@RequestParam int userId){
        return getAuthorService.getAuthor(userId);
    }

    //文章详情页
    @GetMapping("/getarticle")
    public Result getArticle(@RequestParam int articleId){

        Article article = articleMapper.selectById(articleId);
        if(!Objects.isNull(article)){
            return new Result(200, "成功获取文章", article);
        }else {
            return new Result(201, "获取文章失败", null);
        }
    }
    //文章添加路由
    @PostMapping("/addurl")
    public Result addArticleUrl(@RequestBody Article article){

        return articleMapper.updateById(article) != 0 ? new Result(200, "文章路由添加成功", null)
                                                      : new Result(201, "文章路由添加失败", null);
    }
    //获取文章种类
    @GetMapping("/category")
    public Result getCategory(){

        List<Category> categories = categoryMapper.selectList(null);
        return Objects.isNull(categories) != true ? new Result(200, "成功获取文章种类", categories)
                                                  : new Result(201, "文章种类获取失败", null) ;
    }
    //新增文章种类
    @PostMapping("/addcategories")
    public Result addCategories(@RequestBody List<String> tags){
        int count = 0;

        for (String tag : tags) {
            LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Category::getCategoryName,tag);
            List<Category> categories = categoryMapper.selectList(lqw);
            if(categories.isEmpty()){
                categoryMapper.insert(new Category().setCategoryName(tag));
                count++;
            }
        }

        return count !=0 ? new Result(200, "文章分类添加成功", null)
                            : new Result(201, "文章分类添加失败", null);
    }
}
